package com.miyake.loveviewweb.client.explorer;

import java.util.HashMap;
import java.util.Map;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.miyake.loveviewweb.shared.model.ChartModel;
import com.miyake.loveviewweb.shared.model.DataModel;
import com.miyake.loveviewweb.shared.model.FolderModel;
interface TreeManagerListener {
    void onSelectionChange(Key object);
};
public class TreeManager {

    private Key selectedFolder;
    private Key selectedData;
    
    public Key getSelectedFolder() {
        return selectedFolder;
    }

    public Key getSelectedData() {
        return selectedData;
    }
    
    protected Tree tree;
    protected Map<Key, TreeItem> map;
    private TreeManagerListener listener;
    
    public TreeManager(final Tree tree, TreeManagerListener listener) {
        this.tree = tree;
        this.listener = listener;
        this.map  = new HashMap<Key, TreeItem>();

        tree.addSelectionHandler(new SelectionHandler<TreeItem>() {
            public void onSelection(SelectionEvent<TreeItem> event) {
                TreeItem selectedItem = TreeManager.this.tree.getSelectedItem();
                Object object = selectedItem.getUserObject();
                if (object.getClass().equals(FolderModel.class)) {
                    selectedFolder =  (Key)object;
                    selectedData = null;
                }
                else if (object.getClass().equals(DataModel.class)) {
                    TreeItem t = selectedItem.getParentItem();
                    while (t.getUserObject() instanceof DataModel) {
                        t = t.getParentItem();
                    }
                    selectedFolder = (Key)t.getUserObject();
                    selectedData = (Key)selectedItem.getUserObject();
                }
                TreeManager.this.listener.onSelectionChange((Key)event.getSelectedItem().getUserObject());
            }
        });
    }

    public void addRootFolder(FolderModel folder) {
        TreeItem treeItem = new TreeItemCreator(NodeImages.INSTANCE.folder_closed(), folder).getTreeItem();
        treeItem.setStylePrimaryName(tree.getStylePrimaryName());
        tree.addItem(treeItem);
        map.put(folder.getKey(), treeItem);
    }

    public void addSubFolder(Key parent, FolderModel folder) {
        TreeItem treeItem2 = new TreeItemCreator(NodeImages.INSTANCE.folder_closed(), folder).getTreeItem();
        treeItem2.setStylePrimaryName(tree.getStylePrimaryName());
        TreeItem treeItem = map.get(parent);
        
        treeItem.addItem(treeItem2);
        treeItem.setState(true);
        map.put(folder.getKey(), treeItem2);
    }
    
    public void addData(Key parent, DataModel data) {
        TreeItem treeItem2 = new TreeItemCreator(NodeImages.INSTANCE.icon_doc_sml(), data).getTreeItem();
        treeItem2.setStylePrimaryName(tree.getStylePrimaryName());
        TreeItem treeItem = map.get(parent);
        treeItem.addItem(treeItem2);
        treeItem.setState(true);        
        map.put(data.getKey(), treeItem2);
    }

    public void addChart(Key parent, ChartModel chart) {
        TreeItem treeItem2 = new TreeItemCreator(NodeImages.INSTANCE.graph1(), chart).getTreeItem();
        treeItem2.setStylePrimaryName(tree.getStylePrimaryName());
        TreeItem treeItem = map.get(parent);
        treeItem.addItem(treeItem2);
        treeItem.setState(true);        
        map.put(chart.getKey(), treeItem2);  
    }

    public void remove(Key key) {
        TreeItem treeItem = map.get(key);
        treeItem.remove();
    }

    public void removeSub(Key key) {
        TreeItem treeItem = map.get(key);
        for (int i = 0 ; i < treeItem.getChildCount(); i++) {
            treeItem.removeItem(treeItem.getChild(i));
        }
    }


}